package com.xiaoxi.open.common.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName RedisUtils
 * @Description 存取 Redis 值
 * @Author xiaoxi
 * @Date 2022/4/24 14:40
 * @Version 1.0
 */
@Component
public class RedisUtils {

    @Resource
    private RedisTemplate redisTemplate;

    private static final double size = Math.pow(2, 32);


    /**
     * 获取key还有多少时间过期
     *
     * @param key
     * @return
     */
    public long getKeyExpire(final String key) {
        if (exists(key)) {
            return redisTemplate.getExpire(key);
        } else {
            return 0;
        }
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存设置时效时间
     *
     * @param key
     * @param value
     * @param expireTime 秒
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除对应的key
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }


    /**
     * 集合添加
     *
     * @param key
     * @param value
     */
    public void addMembers(final String key, Object value) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add(key, value);
    }

    /**
     * 集合获取
     *
     * @param key
     * @return
     */
    public Set<Object> getMembers(final String key) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        return set.members(key);
    }

    /**
     * 根据key移除某个value
     *
     * @param key
     * @return
     */
    public void removeMembers(final String key, String value) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.remove(key, value);
    }


    /**
     * sort set 添加元素 ---
     * 注意，注意：如果key value 存在的情况，他会实现覆盖，而非出现两对key value
     *
     * @param key
     * @param value
     * @param score
     */
    public void addZSetScore(final String key, Object value, Double score) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        zSet.add(key, value, score);
    }

    /**
     * sort set 分数范围内的数据
     *
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Set<Object> getZSetRangeScore(final String key, double min, double max) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        return zSet.rangeByScore(key, min, max);
    }

    /**
     * 清除数据 分数范围内的数据
     *
     * @param key
     * @param min
     * @param max
     */
    public void clearZSetRemainingData(final String key, double min, double max) {
        ZSetOperations<String, Object> zSetOps = redisTemplate.opsForZSet();
        zSetOps.removeRangeByScore(key, min, max);
    }

    /**
     * 获取某个zSet value 的分数
     *
     * @param key
     * @param value
     * @return
     */
    public Double getZSetScoreByValue(final String key, Object value) {
        ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
        // 获取Sorted Set中指定元素的分数
        return zSetOperations.score(key, value);
    }


    /**
     * 移除sort set value
     *
     * @param key
     * @param value
     * @return
     */
    public void removeZSetValue(final String key, Object... value) {
        ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.remove(key, value);
    }

    /**
     * 批量写入redis
     *
     * @param batch
     */
    public void batchSet(Map<String, Object> batch) {
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        operations.multiSet(batch);
    }
}
